//
//  MasterViewController.m
//  FinAppsParty App
//
//  Created by Ivan Paquico on 11/11/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "MasterViewController.h"

#import "DetailViewController.h"
#import "ViewOcupation.h"

@implementation MasterViewController

@synthesize detailViewController = _detailViewController;
@synthesize viewOcupation = _viewOcupation;
@synthesize menu;
@synthesize appDelegate;
@synthesize cell;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
     //   self.title = NSLocalizedString(@"Master", @"Master");
        self.clearsSelectionOnViewWillAppear = NO;
        self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
        self.appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    }
    return self;
}
							
- (void)dealloc
{
    [_detailViewController release];
    [_viewOcupation release];
    [cell release];

    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
      
    self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);

    
    self.menu = [[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"menu" ofType:@"plist"]] retain];
    
    NSLog(@"Menu content: %@", menu);
 
    self.viewOcupation = [[[ViewOcupation alloc] initWithNibName:@"ViewOcupation" bundle:nil] retain];
    
    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionMiddle];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.menu count];
}



// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   /* static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }*/

    static NSString *MyIdentifier = @"cell";
    UITableViewCell *_cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (_cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
        _cell = cell;
    }

    
    // Configure the cell.
    //_cell.textLabel.text =
    
    UILabel *label;
    label = (UILabel *)[_cell viewWithTag:2];
    
  
    label.text =  [menu objectAtIndex:indexPath.row];
    
    UILabel *label2;
    label2 = (UILabel *)[_cell viewWithTag:3];
    
    UILabel *label3;
    label3 = (UILabel *)[_cell viewWithTag:4];
    


    
    UIImageView *background;
    background = (UIImageView *)[_cell viewWithTag:89];
    
    if (indexPath.row == 0) {
        background.image =  [UIImage imageWithContentsOfFile: 
                             [[NSBundle mainBundle] 
                              pathForResource:@"Menu1" 
                              ofType:@"png" 
                              inDirectory: @"/"]];
        label.textColor = [UIColor colorWithRed:1 green:0.831372549 blue:0.474509804 alpha:1];
        label2.textColor = [UIColor colorWithRed:1 green:0.831372549 blue:0.474509804 alpha:1];
        label2.text =  @"C";
        label3.text = @"Localización, ocupación, antigüedad, superficie";
        label3.textColor = [UIColor whiteColor];
    }else if (indexPath.row == 1) {
        background.image =  [UIImage imageWithContentsOfFile: 
                             [[NSBundle mainBundle] 
                              pathForResource:@"Menu2" 
                              ofType:@"png" 
                              inDirectory: @"/"]];
          label.textColor = [UIColor colorWithRed:0.556862745 green:0.458823529 blue:0.160784314 alpha:1];
          label2.textColor = [UIColor colorWithRed:0.556862745 green:0.458823529 blue:0.160784314 alpha:1];
           label3.text = @"Acabados, carpinterías";
        label3.textColor = [UIColor blackColor];

         label2.text =  @"S";
    }else if (indexPath.row == 2) {
        background.image =  [UIImage imageWithContentsOfFile: 
                             [[NSBundle mainBundle] 
                              pathForResource:@"Menu3" 
                              ofType:@"png" 
                              inDirectory: @"/"]];
        label.textColor = [UIColor whiteColor];
        label2.textColor = [UIColor whiteColor];
        label3.text = @"Acabados, carpinterías, equipamiento";
        label3.textColor = [UIColor whiteColor];

        label2.text =  @"K";
    }else if (indexPath.row == 3) {
        background.image =  [UIImage imageWithContentsOfFile: 
                             [[NSBundle mainBundle] 
                              pathForResource:@"Menu4" 
                              ofType:@"png" 
                              inDirectory: @"/"]];
        label.textColor = [UIColor blackColor];
        label2.textColor = [UIColor blackColor];
        label3.text = @"Acabados, carpinterías, equipamiento";
        label3.textColor = [UIColor blackColor];

        label2.text =  @"B";
    }else if (indexPath.row == 4) {
        background.image =  [UIImage imageWithContentsOfFile: 
                             [[NSBundle mainBundle] 
                              pathForResource:@"Menu5" 
                              ofType:@"png" 
                              inDirectory: @"/"]];
        label.textColor = [UIColor colorWithRed:1 green:0.831372549 blue:0.474509804 alpha:1];
        label2.textColor = [UIColor colorWithRed:1 green:0.831372549 blue:0.474509804 alpha:1];
        label3.textColor = [UIColor whiteColor];

 label3.text = @"Acabados, carpinterías";
        label2.text =  @"D";
    }else if (indexPath.row ==5) {
        background.image =  [UIImage imageWithContentsOfFile: 
                             [[NSBundle mainBundle] 
                              pathForResource:@"Menu6" 
                              ofType:@"png" 
                              inDirectory: @"/"]];
        label.textColor = [UIColor colorWithRed:1 green:0.282352941 blue:0.141176471 alpha:1];
        label2.textColor = [UIColor colorWithRed:1 green:0.282352941 blue:0.141176471 alpha:1];
        label3.textColor = [UIColor blackColor];
        label3.text = @"Entrada, duración";
        label2.text =  @"H";
    }else if (indexPath.row ==6) {
        background.image =  [UIImage imageWithContentsOfFile: 
                             [[NSBundle mainBundle] 
                              pathForResource:@"Menu7" 
                              ofType:@"png" 
                              inDirectory: @"/"]];
        label.textColor = [UIColor colorWithRed:1 green:0.282352941 blue:0.141176471 alpha:1];
        label2.textColor = [UIColor colorWithRed:1 green:0.282352941 blue:0.141176471 alpha:1];
        label3.textColor = [UIColor blackColor];
        label3.text = @"";
        label2.text =  @"";
        label.text = @"";
    }
    
    
    
    
    //_cell.accessoryType = UITableViewCellAccessoryNone;
    
    return _cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Click!");
    
    /*
    if (!self.detailViewController) {
        self.detailViewController = [[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil] autorelease];
    }*/
   // _detailViewController.codigoPostal.text = @"123";


   // [self.navigationController pushViewController:self.detailViewController animated:YES];
    
    
/*
    if (!self.detailViewController) {
        self.detailViewController = [[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil] retain];
    }
    if (!self.viewOcupation) {
        self.viewOcupation = [[[ViewOcupation alloc] initWithNibName:@"ViewOcupation" bundle:nil] retain];
    }
    
   // [self.appDelegate.splitViewController viewWillDisappear:YES];
    /*NSMutableArray *viewControllerArray=[[NSMutableArray alloc] initWithArray:[[self.appDelegate.splitViewController.viewControllers objectAtIndex:0] viewControllers]];
	[viewControllerArray removeLastObject];*/


 //  [self.appDelegate.splitViewController viewWillDisappear:YES];
    
 //   if (indexPath.row == 0) {
		//self.firstDetailViewController=[[[FirstDetailViewController alloc] init]autorelease];
		//[viewControllerArray addObject:self.firstDetailViewController];
	 	//self.appDelegate.splitViewController.delegate = self.firstDetailViewController;
       //  [self.appDelegate.splitViewController viewWillDisappear:YES];
   //     [self.navigationController pushViewController:self.detailViewController animated:YES];
      // [self.splitViewController setViewControllers:[NSArray arrayWithObjects:self.navigationController, self.detailViewController, nil]];
      //[viewControllerArray addObject:self.detailViewController];
		//self.appDelegate.splitViewController.delegate = self.detailViewController;
      //  [[self.appDelegate.splitViewController.viewControllers objectAtIndex:1] setViewControllers:viewControllerArray animated:NO];
           
        /*MyCostomViewController *cus = [[MyCostomViewController alloc] initWithNibName:@"MyCostomViewController" bundle:nil];*/
       // CustomViewController* viewController = [[CustomViewController alloc] init];
		
		//[self.appDelegate.splitViewController pushViewController:self.viewOcupation animated:YES];
		//[viewController release];
       // [self.detailViewController becomeFirstResponder];
      //  [super.detailViewController changePage:indexPath.row+1];
        
        
	//}
	
    //if (indexPath.row == 1) {
		//self.secondDetailViewController=[[[SecondDetailViewController alloc]init]autorelease];
		//[viewControllerArray addObject:self.secondDetailViewController];
		//self.appDelegate.splitViewController.delegate = self.secondDetailViewController;
       // [self.navigationController pushViewController:self.viewOcupation animated:YES];
        //[self.view addSubview:self.viewOcupation];
       // [self.splitViewController setViewControllers:[NSArray arrayWithObjects:self.navigationController, self.viewOcupation, nil]];
       // [viewControllerArray addObject:self.viewOcupation];
		//self.appDelegate.splitViewController.delegate = self.viewOcupation;
        
 //[[self.appDelegate.splitViewController.viewControllers objectAtIndex:1] setViewControllers:viewControllerArray animated:NO];
       // [self.detailViewController becomeFirstResponder];
      //  self.detailViewController.codigoPostal.text = @"123";

        //[super.detailViewController changePage:indexPath.row+1];
//    }

   
/*
    [[self.appDelegate.splitViewController.viewControllers objectAtIndex:1] setViewControllers:viewControllerArray animated:NO];	
	 [self.appDelegate.splitViewController viewWillAppear:YES];
	
	//[self.appDelegate.splitViewController viewWillAppear:YES];
[viewControllerArray release];
  
    */
}
@end
